package com.eqdd.yiqidian.ui;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SplashActivityCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseApplication;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.StatusBarUtil;
import com.eqdd.yiqidian.web.WebActivity;
import com.jakewharton.rxbinding.view.RxView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class SplashActivity extends BaseActivity implements Animation.AnimationListener {


    private static final int MAX_COUNT = 0;
    private SplashActivityCustom dataBinding;
    private Disposable mDisposable;
    private boolean flag;


    @Override
    public void initBinding() {
        getWindow().setBackgroundDrawable(null);
        initStatus();
        StatusBarUtil.setStatusBarDarkMode(this, true);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
    }

    @Override
    public void initData() {
        OkGo.<HttpResult<Ad>>post(HttpConfig.BASE_URL + HttpConfig.GET_SPLASH_AD)
                .execute(new JsonCallBack<HttpResult<Ad>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<Ad>> response) {
                        HttpResult<Ad> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            ImageUtil.setImage(httpResult.getItems().getUrl(), dataBinding.ivBg);
                            SPUtil.saveAd(httpResult.getItems().getUrl());
                            // SPUtil.saveAdLink(httpResult.getItems().getToUrl());
                            Log.w("xxx url  ", httpResult.getItems().getUrl());
                        }
                    }
                });
    }

    private void initStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decoderView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decoderView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            //or ?
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    public void setView() {
        enterActivity();
        /*flag = SPUtil.getFirst(AppConstent.first_open_app);
        if (flag) {
            startAnim();
        } else {
            dataBinding.tvCount.setVisibility(View.VISIBLE);
            Glide.with(this)
                    .load(SPUtil.getAd())
                    .into(dataBinding.splash);

            Log.w("xxx url -> ", SPUtil.getAd());
            RxView.clicks(dataBinding.tvCount)
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe(aVoid -> {
                        if (mDisposable != null) {
                            mDisposable.dispose();
                        }
                        enterActivity();
                    });
            RxView.clicks(dataBinding.splash)
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe(aVoid -> {
                        if (mDisposable != null) {
                            mDisposable.dispose();
                        }
                        Bundle bundle = new Bundle();
                        bundle.putString(Config.URL, "http://www.eqidd.com");
                        startActivityForResult(WebActivity.class, bundle, Config.WEB);
                    });
            mDisposable = Flowable.interval(1, 1, TimeUnit.SECONDS)
                    .map(aLong -> MAX_COUNT - aLong)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(consumer);
        }*/

    }

    Consumer consumer = new Consumer<Long>() {
        @Override
        public void accept(Long aLong) throws Exception {

            if (aLong >= 0) {
                dataBinding.tvCount.setText(aLong + "秒");
            } else {
                enterActivity();
            }

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }

    private void startAnim() {
        AlphaAnimation anim = new AlphaAnimation(0, 1);
        anim.setDuration(1500);
        anim.setFillAfter(true);
        anim.setAnimationListener(this);
        dataBinding.splash.startAnimation(anim);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO:
        enterActivity();
    }

    private void enterActivity() {

        if (flag) {
            Intent guide = new Intent(this, GuideActivity.class);
            startActivity(guide);
            finish();
        } else {
            Intent login = new Intent(this, LoginActivity.class);
            startActivity(login);
            finish();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        enterActivity();
    }

    class Ad {

        /**
         * id : 4
         * url : https://www.eqid.top:8009/image/adImage/171106143115.png
         * createTime : 2017-11-06T14:31:15.067
         * toUrl :
         */

        private int id;
        private String url;
        private String createTime;
        private String toUrl;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getCreateTime() {
            return createTime;
        }

        public void setCreateTime(String createTime) {
            this.createTime = createTime;
        }

        public String getToUrl() {
            return toUrl;
        }

        public void setToUrl(String toUrl) {
            this.toUrl = toUrl;
        }
    }
}

